﻿
using IBll;
using IDal;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Bll
{
    public class EmployeeBll : IEmployeeBll
    {
         public EmployeeBll(IStatusDal<Employee> statusDa,IStatusDal<Status> statusD)
        {
            StatusDa = statusDa;
            StatusD = statusD;//状态
        }

        public IStatusDal<Employee> StatusDa { get; }
        public IStatusDal<Status> StatusD { get; }

        public async Task<int> AddAnync(Employee employee)
        {
            string a = DateTime.Now.ToString("yyyyMMdd");
            string m = new Random().Next(100,1000).ToString();
            employee.Code = a + m;


           return await StatusDa.AddAnync(employee);

        }

        public Employee FindEmployee(int id)
        {
            return StatusDa.GetValues().FirstOrDefault(x => x.EmployeeId == id);

        }

        public EnpDto ShwoEmployee(int index, int size)
        {
            var employee = StatusDa.GetValues() ;
            var status = StatusD.GetValues();
            var linq = from a in status
                       join b in employee
                       on a.StatusId equals b.StatusId
                       select new
                       {
                           b.EmployeeName,
                           b.EmployeeId,
                           b.EmployeeDate,
                           b.EmployeePwd,
                           b.Num,
                           b.Code,
                           b.IsDeleted,
                           b.Sex,
                           b.StatusId,
                           a.StatusName,
                          b.EmployeeImg,
                       };

                int p=linq.Count();
            var o = linq.OrderBy(x => x.EmployeeId).Skip((index - 1) * size).Take(size);
             return new EnpDto { Count = p ,Mumber=o};
        }

        public async Task<int> UpdateAnync(Employee employee)
        {
           return await  StatusDa.UpdateAnync(employee);

        }
    }
}
